package com.watson.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.watson.R;
import com.watson.bean.ChooseBean;
import com.watson.utils.LogUtils;

import java.util.ArrayList;

public class SourceZAdapter extends RecyclerView.Adapter<SourceZAdapter.ItemViewHolder> {
    private ArrayList<ChooseBean> mList;
    private Context mContext;
    private ItemClickListener mItemClick;

    public SourceZAdapter(Context mContext, ArrayList<ChooseBean> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    public void setData(ArrayList<ChooseBean> mList) {
        this.mList = mList;
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(ItemClickListener listener) {
        this.mItemClick = listener;
    }

    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemViewHolder itemViewHolder;
        itemViewHolder = new ItemViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_source_z, parent, false));
        return itemViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {
        if (mList.size() > 0) {
            if (mList.get(0).getType() == position) {
                holder.tvHome.setTextColor(mContext.getResources().getColor(R.color.bg_main));
            } else {
                holder.tvHome.setTextColor(mContext.getResources().getColor(R.color.tv333));
            }
            holder.tvHome.setText(mList.get(position).getName());
            holder.itemView.setOnClickListener(view -> {
                if (mItemClick != null) {
                    LogUtils.e("TAG", "position:" + position);
                    LogUtils.e("TAG", "size:" + mList.size());
                    mList.get(0).setType(position);
                    mItemClick.ItemClick(position, mList.get(position));
                    notifyDataSetChanged();
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {
        private TextView tvHome;

        public ItemViewHolder(View itemView) {
            super(itemView);
            tvHome = itemView.findViewById(R.id.tvHome);
        }
    }

    public interface ItemClickListener {

        void ItemClick(int position, ChooseBean string);
    }
}
